package cn.edu.xaut.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * JDBC的工具类
 */
public class JDBCUtils {

    private static final String driverClass;
    private static final String url;
    private static final String username;
    private static final String password;

    static {

        // 加载属性文件并解析
        Properties properties = new Properties();
        // 使用类的加载器的方式获取文件的输入流
        InputStream inputStream = JDBCUtils.class.getClassLoader().
                getResourceAsStream("jdbc.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        driverClass = properties.getProperty("driverClass");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
    }

    private static Connection con = null;
    private static Statement stmt = null;
    private static PreparedStatement pstmt = null;
    private static ResultSet rs = null;
    private static int rows = 0;    // 修改记录的行数

    /**
     * 注册驱动的方法
     * @throws ClassNotFoundException
     */
    public static void loadDriver() throws ClassNotFoundException {
        Class.forName(driverClass);
    }

    /**
     * 获得连接的方法
     * @return Connection
     */
    public static Connection getConnection() throws Exception {
        loadDriver();
        con = DriverManager.getConnection(url, username, password);
        return con;
    }

    /* 以下的查询query()与修改update()可能会出现SQL注入漏洞的风险 */

    /**
     * 查询
     * @param sql
     * @return ResultSet
     */
    public static ResultSet query(String sql) {

        try {
            con = getConnection();
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return rs;
    }

    /**
     * 修改
     * @param sql
     * @return int
     */
    public static int update(String sql) {

        try {
            con = getConnection();
            stmt = con.createStatement();
            rows = stmt.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return rows;
    }


    /**
     * 释放资源
     * @throws SQLException
     */
    public static void release() {
        release(rs, stmt, con);
    }

    public static void release(Statement stmt, Connection con) {

        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }

        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            con = null;
        }
    }

    public static void release(ResultSet rs, Statement stmt, Connection con) {

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }

        release(stmt, con);
    }
}
